package com.regentsoft.cloudback.operateAdmin.controller;

import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.regentsoft.cloudback.base.BaseResp;
import com.regentsoft.cloudback.base.ResponseCode;
import com.regentsoft.cloudback.operateAdmin.entity.SysAdmin;
import com.regentsoft.cloudback.operateAdmin.service.SysAdminService;
import com.regentsoft.cloudback.util.EncryptUtil;
import com.regentsoft.cloudback.vo.req.SysAdminLoginReq;
import com.regentsoft.cloudback.vo.resp.SysAdminLoginResp;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
/**
 * 
 * @author czd
 *
 * @date 2017年11月25日
 *
 * @version 1.0
 */
@Api
@RestController
@RequestMapping("manager/admin/")
public class SysAdminController {
	private final Logger LOGGER = Logger.getLogger(this.getClass());
	@Autowired
	private SysAdminService sysAdminService;
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	/**
	 * 
	 * @param req
	 * @return
	 */
	@ApiOperation(value = "运营登录", httpMethod = "POST", response = SysAdminLoginResp.class, notes = "运营登录")
	@RequestMapping(value="sysLogin")
    public SysAdminLoginResp loginApi(@RequestBody SysAdminLoginReq req) {
		LOGGER.info("运营登录：{}"+req.getName());
		SysAdminLoginResp resp  = new SysAdminLoginResp();
		SysAdmin admin = null;
		try {
			admin = sysAdminService.findByName(req.getName());
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(),e);
		}
		if(admin==null) {
			resp.setCode(ResponseCode.PASSORACCOUNTERR);
			resp.setMessage("账号密码错误！");
			return resp;
		}
		if(admin.getPassword().equals(EncryptUtil.EncryptEx(req.getPassword()))) {
			resp.setCode(ResponseCode.SUCCESS);
			resp.setToken(UUID.randomUUID().toString());
		}else {
			resp.setCode(ResponseCode.PASSORACCOUNTERR);
			resp.setMessage("账号密码错误！");
			return resp;
		}
		stringRedisTemplate.opsForValue().set("SYS_TOKEN_"+resp.getToken(), resp.getToken());
        return resp;
    }
	/**
	 * 
	 * @param req
	 * @return
	 */
	@ApiOperation(value = "退出登录", httpMethod = "GET", response = BaseResp.class, notes = "退出登录")
	@RequestMapping(value="sysExit")
    public BaseResp sysExit(@RequestParam String token) {
		BaseResp resp  = new BaseResp();
		stringRedisTemplate.delete("SYS_TOKEN_"+token);
		resp.setCode(ResponseCode.SUCCESS);
		resp.setMessage("退出登陆成功");
        return resp;
    }
	public static void main(String[] args) {
		System.err.println(EncryptUtil.EncryptEx("123456"));
	}
	
}
